import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 对于TreeMap的一些简单了解
 * User: xwl
 * Date: 2023-06-01
 * Time: 18:53
 */

public class Test {

    public static void main(String[] args) {
        Map<String, Integer> treeMap = new TreeMap<>();
        // 4、设置key和对应的value
        treeMap.put("hello",3);
        treeMap.put("abc",4);
        treeMap.put("the",8);
        System.out.println(treeMap);
        // 1、get返回key对应的value
        Integer val = treeMap.get("hello");
        System.out.println(val);
        // 2、返回key对应的value，key不存在，返回默认值
        Integer val2 = treeMap.getOrDefault("val2",200);
        System.out.println(val);
        // 3、返回所有key的不重复集合
        Set<String> keySet = treeMap.keySet();
        System.out.println(keySet);
        // 4、删除key对应的映射关系
        treeMap.remove("hello");
        treeMap.remove("the",8);
        System.out.println(treeMap);
        // 5、返回所有的key-value的映射关系
        Set<Map.Entry<String,Integer>> set = treeMap.entrySet();
        for (Map.Entry<String,Integer> entry : set) {
            System.out.println("key:"+entry.getKey()+ '\n' +"value:" + entry.getValue());
        }
        // Map中存放的key是唯一的，再放相同key时value会被替换
        treeMap.put("abc",1);
        System.out.println(treeMap);
        treeMap.put("abc",14);
        System.out.println(treeMap);
        // key不能重复
        Set<String> set2 = new TreeSet<>();
        set2.add("asd");
        set2.add("as");
        set2.add("as");
        System.out.println(set2);
    }
}
